package upgrade

import (
	"context"
	"strings"
	"time"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/upgrade"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevOtaFireware = cUpgradeDev_ota_fireware{}

type cUpgradeDev_ota_fireware struct{}

// GetDevOtaFirewareList 获取列表
func (u *cUpgradeDev_ota_fireware) GetDevOtaFirewareList(ctx context.Context, req *upgrade.GetDevOtaFirewareListReq) (res *upgrade.GetDevOtaFirewareListRes, err error) {
	var reqData = new(model.GetDevOtaFirewareListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.UpgradeDevOtaFireware().GetDevOtaFirewareList(ctx, reqData)
	res = new(upgrade.GetDevOtaFirewareListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevOtaFirewareById 获取指定ID数据
func (u *cUpgradeDev_ota_fireware) GetDevOtaFirewareById(ctx context.Context, req *upgrade.GetDevOtaFirewareByIdReq) (res *upgrade.GetDevOtaFirewareByIdRes, err error) {
	data, err := service.UpgradeDevOtaFireware().GetDevOtaFirewareById(ctx, req.Id)
	res = new(upgrade.GetDevOtaFirewareByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddDevOtaFireware 添加数据
func (u *cUpgradeDev_ota_fireware) AddDevOtaFireware(ctx context.Context, req *upgrade.AddDevOtaFirewareReq) (res *upgrade.AddDevOtaFirewareRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaFirewareAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.UpgradeDevOtaFireware().AddDevOtaFireware(ctx, data)
	return
}

// EditDevOtaFireware 修改数据
func (u *cUpgradeDev_ota_fireware) EditDevOtaFireware(ctx context.Context, req *upgrade.EditDevOtaFirewareReq) (res *upgrade.EditDevOtaFirewareRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaFirewareEditInput{}
	gconv.Scan(req, &data)
	data.CreatedBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.UpgradeDevOtaFireware().EditDevOtaFireware(ctx, data)
	return
}

// UploadFireware 上传固件
func (u *cUpgradeDev_ota_fireware) UploadFireware(ctx context.Context, req *upgrade.UploadReq) (res *upgrade.UploadRes, err error) {
	if req.Files == nil {
		err = gerror.New("请上传文件")
		return
	}
	dir := "./upload/fireware/" + time.Now().Format("2006")
	names, err := req.Files.Save(dir, false)

	if err != nil {
		return
	}

	fileNames := []string{}
	for k := 0; k < len(names); k++ {
		names[k] = strings.TrimLeft(dir, ".") + "/" + names[k]
		fileNames = append(fileNames, names[k])
	}
	res = new(upgrade.UploadRes)
	res.Url = strings.Join(fileNames, ",")
	return
}

// DeleteDevOtaFireware 删除数据
func (u *cUpgradeDev_ota_fireware) DeleteDevOtaFireware(ctx context.Context, req *upgrade.DeleteDevOtaFirewareReq) (res *upgrade.DeleteDevOtaFirewareRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.UpgradeDevOtaFireware().DeleteDevOtaFireware(ctx, req.Ids)
	return
}
